<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="600px" append-to-body>
        <el-form ref="form" :model="model" :rules="rules" inline label-width="100px" label-suffix=":">
            <el-form-item label="仓库名称" prop="name">
                <el-input v-model.trim="model.name" placeholder="请输入仓库名称" clearable></el-input>
            </el-form-item>
            <el-form-item label="仓库地址" prop="address">
                <el-input v-model.trim="model.address" placeholder="请输入仓库地址" clearable></el-input>
            </el-form-item>
            <el-form-item label="负责人" prop="admin_id">
                <el-input v-model.trim="model.admin_id" placeholder="请输入负责人" clearable></el-input>
            </el-form-item>
            <el-form-item label="默认状态" prop="status">
                <el-switch v-model="model.status" :active-value="1" :inactive-value="0" active-text="设为默认" inactive-text="初始"></el-switch>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" :loading="save" :disabled="save" @click="submit">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { warehouseFind, warehouseAdd, warehouseEdit } from '@/api/warehouse'
export default {
    name: 'DetailDialog',
    props: {
        value: {
            type: Boolean,
            default: false,
            required: true,
        },
        params: {
            type: Object,
            default: () => {},
        },
        type: {
            type: String,
            default: '',
        },
    },
    data() {
        return {
            model: {
                id: '',
                name: '',
            },
            rules: {
                
            },
            dict: {
                platformList: [],
                advistementList: []
            },
            save: false,
        }
    },
    computed: {
        title() {
            return `${this.type == 'edit' ? '编辑' : '新增'}仓库信息`
        },
        dialogVisible: {
            get() {
                return this.value
            },
            set(val) {
                this.$emit('input', val)
            },
        },
    },
    watch: {
        params: {
            handler(val) {
                if (val.id) {
                    this.getDetail()
                }
            },
            immediate: true,
            deep: true,
        },
    },
    async mounted() {
    },
    methods: {
        getDetail() {
            this.loading = true
            const params = { id: this.params.id }
            warehouseFind(params)
                .then(res => {
                    this.model = Object.assign({}, this.model, res.data)
                })
                .finally(() => {
                    this.loading = false
                })
        },
        submit() {
            this.$refs.form.validate(valid => {
                if (!valid) {
                    return
                }
                const params = { ...this.model }
                if (this.save) {
                    return
                }
                this.save = true
                if (this.type === 'edit') {
                    warehouseEdit(params)
                        .then(res => {
                            this.$message.success(res.msg)
                            this.dialogVisible = false
                            this.$emit('success')
                        })
                        .finally(() => {
                            this.save = false
                        })
                } else {
                    warehouseAdd(params)
                        .then(res => {
                            this.$message.success(res.msg)
                            this.dialogVisible = false
                            this.$emit('success')
                        })
                        .finally(() => {
                            this.save = false
                        })
                }
            })
        },
        cancel() {
            this.dialogVisible = false
        },
    },
}
</script>

<style>
</style>
